package com.higgins.lawapp.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.higgins.lawapp.client.application.dataModel.LawFirm;
import com.higgins.lawapp.client.application.rpc.LawFirmService;
import com.higgins.lawapp.client.application.rpc.LawFirmServiceAsync;

public class LawFirmTable extends FlexTable	{
	private final LawFirmServiceAsync lawFirmService = GWT.create(LawFirmService.class);
	private String coveringLetter = null;
	
	public LawFirmTable()	{
		super();
		initialise();
	}
	public void initialise()	{
		this.setCellPadding(1);
		this.setCellSpacing(0);
		this.setWidth("800px");
		
		addCheckBox(0,0, false, null);
		this.setText(0,1,"Law Firm");
		this.setText(0,2,"Address");
		this.setText(0,3,"Contact");
		
		RowFormatter rf = this.getRowFormatter();
		rf.setStyleName(0, "FlexTable-HeaderRow");
		this.getCellFormatter().addStyleName(0, 0,"FlexTable-checkBoxCell");
		this.getCellFormatter().addStyleName(0, 1,"FlexTable-normCell");
		this.getCellFormatter().addStyleName(0, 2,"FlexTable-normCell");
		this.getCellFormatter().addStyleName(0, 3,"FlexTable-normCell");
	}
	private void addCheckBox(int row, int column, Boolean apply, Long id)	{
		CheckBox checkBox = new CheckBox();
		checkBox.setName(id!=null?id.toString():"null");
		
		setWidget(row, column, checkBox);
		checkBox.setValue(apply!=null?apply:false);
		getCellFormatter().addStyleName(row, column, "FlexTable-checkBoxCell");
		addCheckBoxListener(checkBox);
	}
	private void addCheckBoxListener(CheckBox checkBox)	{
		checkBox.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				Object source = event.getSource();
				CheckBox chkBox = (CheckBox) source;
				String name = chkBox.getName();
				Long id = Long.parseLong(name);
				lawFirmService.updateLawFirmPublished(id, coveringLetter, chkBox.getValue(), new AsyncCallback<Boolean>()	{

					@Override
					public void onFailure(Throwable caught) {
						System.out.println("FAILURE " + caught.toString());
					}

					@Override
					public void onSuccess(Boolean result) {
						System.out.println("SUCCESS");
					}
					
				});
				
				System.out.println("CHECKBOX:::::::");
				System.out.println(name);
			}
			
		});
	}
	public void setLawFirms(List<LawFirm> lawFirms)	{
		clearTable();
		int row = 1;
		for (LawFirm lawFirm:lawFirms)	
			addLawFirm(lawFirm, row++);
	}
	private void clearTable()	{
		int rows = getRowCount();
		for (int row = 1; row < rows; row++)
			this.removeRow(getRowCount()-1);
//		initialise();		
	}
	public void addLawFirm(LawFirm lawFirm, int row)	{
		int column = 0;
		int rowCount = this.getRowCount();
		if (rowCount <= row)
			this.insertRow(rowCount);
		addCheckBox(row, column++, isApplySelected(lawFirm), lawFirm.getId());
		addLawFirmName(row, column++, lawFirm.getLawFirm());
		addAddress(row, column++, lawFirm.getAddress());
		addContact(row, column++, lawFirm.getContact());
	}
	private Boolean isApplySelected(LawFirm lawFirm)	{
		try	{
			return (lawFirm.getApply() && coveringLetter.equals(lawFirm.getCoveringLetter()));
		} catch (Exception e)	{
			return false;
		}
	}
	private void addLawFirmName(int row, int column, String lawFirmName)	{
		this.setText(row,column,lawFirmName);
		getCellFormatter().addStyleName(row, column, "FlexTable-normCell");
	}
	private void addAddress(int row, int column, String address)	{
		this.setText(row, column,address);
 		getCellFormatter().addStyleName(row, column, "FlexTable-normCell");
	}
	private void addContact(int row, int column, String contact)	{
		this.setText(row, column, contact);
		getCellFormatter().addStyleName(row++, column, "FlexTable-normCell");
	}
	
	public void setCoveringLetter(String coveringLetter)	{
		this.coveringLetter = coveringLetter;
	}
//		RowFormatter rfNorm = this.getRowFormatter();
//		rfNorm.setStyleName(row, "FlexTable-NormRow");
	
}
